package com.shengyin.ec.controller.shopcart;

import java.util.ArrayList;
import java.util.List;

import com.shengyin.ec.model.entity.Goods;

public class ShopCartForm {

	// 购物车的商品及对应的购买数量
	private ArrayList<Item> itemList = new ArrayList<Item>();
	
	float boughtMarketItemprice=0;
	float boughtItemprice=0;
	
	// 购物车记录数
	private int itemSize = 0;
	// 购物车商品总数量
	private int productTotalQuantity = 0;

	// 购物车总价
	private float shopCartTotalPrice = 0;

	// 购物车商品的市场总价
	private float shopCartMarketyPrice = 0;

	public ArrayList<Item> getItemList() {
		return itemList;
	}

	public void setItemList(ArrayList<Item> itemList) {
		this.itemList = itemList;
	}

	public int getItemSize() {
		return itemSize;
	}

	public void setItemSize(int itemSize) {
		this.itemSize = itemSize;
	}

	public int getProductTotalQuantity() {
		return productTotalQuantity;
	}

	public void setProductTotalQuantity(int productTotalQuantity) {
		this.productTotalQuantity = productTotalQuantity;
	}

	public float getShopCartTotalPrice() {
		return shopCartTotalPrice;
	}

	public void setShopCartTotalPrice(float shopCartTotalPrice) {
		this.shopCartTotalPrice = shopCartTotalPrice;
	}

	public float getShopCartMarketyPrice() {

		return shopCartMarketyPrice;
	}

	public void setShopCartMarketyPrice(float shopCartMarketyPrice) {
		this.shopCartMarketyPrice = shopCartMarketyPrice;
	}

	public void remove(Goods goods) {

		Item item = this.isItemExistShopCart(goods);
		this.itemList.remove(item);
		this.shopCartTotalPrice -= item.getSubtotal();
		this.shopCartMarketyPrice -= item.getMarketSubtotal();
		this.productTotalQuantity -= item.getQuantity();

	}

	public void addItem(Item item) {
		Item ite = this.isItemExistShopCart(item.getGoods());
		if (ite != null) {
			int quantity = ite.getQuantity();
			ite.setQuantity(quantity + item.getQuantity());
			return;
		}
		ite = item;
		this.itemList.add(ite);
	}

	// 判断购物车中是否存在待添加的商品
	Item isItemExistShopCart(Goods goods) {

		if (goods == null)
			return null;

		for (Item item : this.itemList) {
			if (item.getGoods().getId().equals(goods.getId())) {
				return item;
			}
		}

		return null;
	}

	// 计算购物车总价
	public void caculateTotalMarketPrice(Item item) {
		if (item == null){
			this.shopCartMarketyPrice=0;
			for(Item ite:this.getItemList()){
				shopCartMarketyPrice += ite.getMarketSubtotal();
			}
			return;
		}
		
		shopCartMarketyPrice += item.getMarketSubtotal();
	}

	// 计算购物车总价
	public void caculateTotalPrice(Item item) {
		if (item == null){
			this.shopCartTotalPrice=0;
			for(Item ite:this.getItemList()){
				this.shopCartTotalPrice += ite.getSubtotal();
			}
			return;
		}
			
		shopCartTotalPrice += item.getSubtotal();
	}
//计算购物车总数量
	public void caculateTotalGoodsNum(Item item) {
		if (item == null){
			this.productTotalQuantity=0;
			for(Item ite:this.getItemList()){
				this.productTotalQuantity += ite.getQuantity();		
				}
			
			return;
		}
		
			
		this.productTotalQuantity += item.getQuantity();
	}

	
	//获得已购买的商品
	public List<Item> getBoughtGoods(List<Goods> goodsList) {
		List<Item> itemList =this.itemList;
		//选择已购商品
		ArrayList<Item> boughtItemList =new ArrayList<Item>();
		
		for(int i=0;i<itemList.size();i++){
			
			for(int j=0;j<goodsList.size();j++){
				if(itemList.get(i).getGoods().getId().equals(goodsList.get(j).getId())){
					boughtItemList.add(itemList.get(i));
				}
			}
		}
         return boughtItemList;
	}
	
	
	//清除已购买的商品
		public void clearBoughtGoods(List<Goods> goodsList) {
			List<Item> itemList =this.itemList;
			
			for(int i=0;i<itemList.size();i++){
				
				for(int j=0;j<goodsList.size();j++){
					if(itemList.get(i).getGoods().getId().equals(goodsList.get(j).getId())){
						itemList.remove(i);
					}
				}
			}
		}
	
	//计算已购商品总价
	public void caculateBoughtItemPrice(List<Item> itemList2){
		 boughtItemprice=0;
		for(Item item:itemList2){
			boughtItemprice+=item.getSubtotal();
		}
	}
	
	
	//计算已购商品总价
		public void caculateBoughtItemMarketPrice(List<Item> itemList2){
			 boughtMarketItemprice=0;
			for(Item item:itemList2){
				boughtMarketItemprice+=item.getMarketSubtotal();
			}
		}
	
	//计算修改的已购商品数量
		public void updateQuantity(int quantity,Long goodsid){
			List<Item> itemList =this.itemList;
			
			for(int i=0;i<itemList.size();i++){
				
				if(goodsid.equals(itemList.get(i).getGoods().getId())){
					itemList.get(i).setQuantity(quantity);
				}
				
			}
		}

		public float getBoughtMarketItemprice() {
			return boughtMarketItemprice;
		}

		public void setBoughtMarketItemprice(float boughtMarketItemprice) {
			this.boughtMarketItemprice = boughtMarketItemprice;
		}

		public float getBoughtItemprice() {
			return boughtItemprice;
		}

		public void setBoughtItemprice(float boughtItemprice) {
			this.boughtItemprice = boughtItemprice;
		}
		
		
}
